<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRoleClicked"
              >新增角色</el-button>
            </el-row>

            <!-- 表格 -->
            <el-table border :data="roleList">
              <el-table-column prop="id" label="序号" width="180" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(scope.row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRole(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="query.page"
                :page-size="query.pagesize"
                :total="total"
                layout="prev,pager,next"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert type="info" show-icon :closable="false">
              对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改
            </el-alert>

            <el-form label-width="120px" style="margin-top: 30px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  class="with400"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  class="with400"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  class="with400"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

        <!-- 新增/编辑弹框 dialog -->
        <role-dialog
          :show-dialog.sync="showDialog"
          :role-id="roleId"
          @clearRoleId="roleId = $event"
          @updateRoles="handlerUpdateRoles"
        />
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRoleById } from '@/api/setting'
import { mapGetters } from 'vuex'
import RoleDialog from './components/role-dialog'

export default {
  name: 'Setting',
  components: {
    RoleDialog
  },
  data() {
    return {
      showDialog: false,
      roleId: '',
      query: {
        page: 1,
        pagesize: 10
      },
      roleList: [],
      total: 0,
      activeName: 'first',
      formData: {}
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  mounted() {
    this.loadRoleList()
    this.getCompanyInfo()
  },
  methods: {
    handlerUpdateRoles() {
      this.showDialog = false
      this.loadRoleList()
    },
    addRoleClicked() {
      this.showDialog = true
    },
    async getCompanyInfo() {
      const formData = await getCompanyInfo(this.companyId)
      this.formData = formData
    },
    currentChange(page) {
      this.query.page = page
      this.loadRoleList()
    },
    async loadRoleList() {
      const { rows, total } = await getRoleList(this.query)
      this.roleList = rows
      this.total = total
    },
    async delRole(id) {
      try {
        await this.$confirm('确认删除该角色？')
        await deleteRoleById(id)
        this.$message.success('删除角色成功')

        // 判断删除列表时，如果是删到了最后一条数据时，强制当前 page 为1
        if (this.roleList.length === 1 && this.query.page !== 1) {
          this.query.page--
        }
        this.loadRoleList()
      } catch (error) {
        this.$message.warning(error.message)
      }
    },
    editRole(roleId) {
      this.roleId = roleId
      this.showDialog = true
    }
  }
}
</script>

<style>
.with400 {
  width: 400px;
}
</style>
